import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Staff from '../views/Staff.vue'
import Goods from '../views/Goods.vue'
import Sell from '../views/Sell.vue'
import Promotion from '../views/Promotion.vue'
import Loading from '../views/Loading.vue'
import Cash from '../views/Cash.vue'
import stroe from '../store/index'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Login',
        component: Login,
        meta: {
            isLogin: false
        }
    },
    {
        path: '/home',
        name: 'Home',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: Home,
        meta: {
            isLogin: true,
            ops: [1, 99]
        },
        children: [{
                path: 'staff',
                name: 'Staff',
                component: Staff,
                meta: {
                    isLogin: true,
                    ops: [99]
                }
            },
            {
                path: 'goods',
                name: 'Goods',
                component: Goods,
                meta: {
                    isLogin: true,
                    ops: [99]
                }
            },
            {
                path: 'sell',
                name: 'Sell',
                component: Sell,
                meta: {
                    isLogin: true,
                    ops: [99]
                }
            },
            {
                path: 'promotion',
                name: 'Promotion',
                component: Promotion,
                meta: {
                    isLogin: true,
                    ops: [99]
                }
            },
            {
                path: 'loading',
                name: 'Loading',
                component: Loading,
                meta: {
                    isLogin: true,
                    ops: [1]
                }
            },
            {
                path: 'cash',
                name: 'Cash',
                component: Cash,
                meta: {
                    isLogin: true,
                    ops: [1]
                }
            },

        ]
    }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    // to 即将要进入的页面  from 从哪儿来   next允许你进行下一步
    console.log(to)
    console.log(from)
    if (to.meta.isLogin == false) {
        next()
    } else {
        // 这里即将要去的页面是需要登录判断的
        if (stroe.state.user.name) {
            if (to.meta.ops.includes(stroe.state.user.op)) {
                next()
            } else {
                router.push('/home')
            }
            // 在next之前 我是不是应该还要判断一下你又没有进入的权限
            // next()
        } else {
            router.push('/login')
        }
    }
})
export default router